package de.gwtprime.client.views.popup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;

public class DepositCoinsPopup extends Composite {

	private static DepositCoinsPopupUiBinder uiBinder = GWT
			.create(DepositCoinsPopupUiBinder.class);

	interface DepositCoinsPopupUiBinder extends
	UiBinder<Widget, DepositCoinsPopup> {
	}

	@UiField Button generateAddressBtn;
	@UiField Label addressLbl;
	
	private ClientFactory clientFactory;

	public DepositCoinsPopup(final ClientFactory clientFactory) {
		initWidget(uiBinder.createAndBindUi(this));
		
		this.clientFactory = clientFactory;
		
		loadAddress();

		generateAddressBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				loadAddress();
			}
		});
	}
	
	public void loadAddress(){
		addressLbl.setText("Loading...");
		
		clientFactory.getPrimeService().retrieveNewAddress(new AsyncCallback<String>() {

			@Override
			public void onSuccess(String result) {
				addressLbl.setText(result);
			}

			@Override
			public void onFailure(Throwable caught) {
				addressLbl.setText("Fehler beim laden");
			}
		});
	}
}
